package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendanceSallaryTemplateAddDTO;
import org.jsola.hr.dto.AttendanceSallaryTemplateUpdateDTO;
import org.jsola.hr.query.AttendanceSallaryTemplateQuery;
import org.jsola.hr.service.IAttendanceSallaryTemplateService;
import org.jsola.hr.vo.AttendanceSallaryTemplateListVO;
import org.jsola.hr.vo.AttendanceSallaryTemplateVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.validation.Valid;


/**
 * 考勤工资模板表
 *
 * @author zhr
 */
@RestController("hrAttendanceSallaryTemplateController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendanceSallaryTemplates")
public class AttendanceSallaryTemplateController implements IAttendanceSallaryTemplateControllerAPI {



    @Autowired
    private IAttendanceSallaryTemplateService attendanceSallaryTemplateService;

    @Override
    @GetMapping("/downloadTemplate")
    public ResponseEntity<Resource> downloadTemplate() {
        // 模板文件
        InputStream is = getClass().getClassLoader().getResourceAsStream("excel/attendanceSalaryTemplate.xlsx");
        ParamException.notNull(is, "customerInfo.xlsx模板没有找到");
        // 编码文件名，防止乱码
        String filenames = new String("潜在客户信息导入表.xlsx".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE,"application/vnd.ms-excel")
                .body(new InputStreamResource(is));
    }

    @Override
    @PostMapping
    public Result<AttendanceSallaryTemplateVO> save(@Valid @RequestBody AttendanceSallaryTemplateAddDTO attendanceSallaryTemplateAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增考勤工资模板表权限校验

        return Result.success(
                attendanceSallaryTemplateService.save(attendanceSallaryTemplateAddDTO, tokenUser)
        );
    }

    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendanceSallaryTemplateUpdateDTO attendanceSallaryTemplateUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改考勤工资模板表权限校验

        int count = attendanceSallaryTemplateService.update(attendanceSallaryTemplateUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{attendanceSallaryTemplateId}")
    public Result<Integer> delete(@PathVariable String attendanceSallaryTemplateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除考勤工资模板表权限校验

        int count = attendanceSallaryTemplateService.deleteByIds(tokenUser, attendanceSallaryTemplateId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendanceSallaryTemplateIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除考勤工资模板表权限校验

        int count = attendanceSallaryTemplateService.deleteByIds(tokenUser, attendanceSallaryTemplateIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendanceSallaryTemplateId}")
    public Result<AttendanceSallaryTemplateVO> findById(@PathVariable String attendanceSallaryTemplateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看考勤工资模板表权限校验

        return Result.success(
            attendanceSallaryTemplateService.selectById(attendanceSallaryTemplateId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendanceSallaryTemplateListVO>> listAll(@Valid AttendanceSallaryTemplateQuery attendanceSallaryTemplateQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看考勤工资模板表权限校验

        return Result.success(
            attendanceSallaryTemplateService.select(attendanceSallaryTemplateQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendanceSallaryTemplateListVO>> page(@Valid AttendanceSallaryTemplateQuery attendanceSallaryTemplateQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            attendanceSallaryTemplateService.selectPage(attendanceSallaryTemplateQuery, tokenUser.getSiteId())
        );
    }


}

